package com.zw.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.commons.utlis.JwtHelper;
import com.zw.object.goods.GoodStock;
import com.zw.object.goods.GoodType;
import com.zw.goods.mapper.GoodTypeMapper;
import com.zw.goods.service.GoodTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.object.result.Result;
import com.zw.object.vo.GoodsAllTypeVo;
import io.github.classgraph.ArrayTypeSignature;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 物资类型表 服务实现类
 * </p>
 *
 * @author zw
 * @since 2023-03-15
 */
@Service
public class GoodTypeServiceImpl extends ServiceImpl<GoodTypeMapper, GoodType> implements GoodTypeService {

    @Override
    public Result simple() {
        LambdaQueryWrapper<GoodType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodType::getStatus, 1);
        wrapper.select(GoodType::getId, GoodType::getType);
        wrapper.orderByAsc(GoodType::getOrderNum);
        List<GoodType> list = baseMapper.selectList(wrapper);
        return Result.ok(list);
    }

    @Override
    public Result goodsTypeList(GoodsAllTypeVo vo) {
        LambdaQueryWrapper<GoodType> wrapper = new LambdaQueryWrapper<>();
        Page<GoodType> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        wrapper.like(StrUtil.isNotBlank(vo.getType()), GoodType::getType, vo.getType());
        wrapper.like(StrUtil.isNotBlank(vo.getCreateBy()), GoodType::getCreateBy, vo.getCreateBy());
        wrapper.eq(vo.getStatus() != null, GoodType::getStatus, vo.getStatus());
        wrapper.orderByAsc(GoodType::getOrderNum);
        Page<GoodType> typePage = baseMapper.selectPage(page, wrapper);
        return Result.ok(typePage);
    }

    @Override
    public Result save(GoodType goodType, HttpServletRequest request) {
        goodType.setCreateBy(JwtHelper.getUsername(request.getHeader("token")));
        int insert = baseMapper.insert(goodType);
        return insert>0?Result.ok():Result.fail();
    }

    @Override
    public Result update(GoodType goodType, HttpServletRequest request) {
        goodType.setCreateBy(JwtHelper.getUsername(request.getHeader("token")));
        int update = baseMapper.updateById(goodType);
        return update>0?Result.ok():Result.fail();
    }

    @Override
    public Result delete(Long[] ids) {
        int delete = baseMapper.deleteBatchIds(Arrays.asList(ids));
        return delete>0?Result.ok():Result.fail();
    }
}
